/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.Observable;

/**
 *
 * @author root
 */
public class ServerListener extends Observable implements Runnable {

    private ServerSocket socketEscuta;
    private boolean run;
    private int portaEscuta = 6667;

    public ServerListener() {
    }

    public ServerListener(int portaEscuta) {
        this.portaEscuta = portaEscuta;
    }

    public void stop() {
        try {
            socketEscuta.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        this.run = false;
        try {
            socketEscuta.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void run() {
        try {
            socketEscuta = new ServerSocket(portaEscuta);
            this.run = true;
            while (run) {
                setChanged();
                notifyObservers(socketEscuta.accept());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
